home *** CD-ROM | disk | FTP | other *** search
- /*
- * Import Module.js before using this file.
- */
-
- // create a namespace for our Utils.
- var BartUtils = BartModule.createNamespace("bart.ibrowser.Utils");
-
- /**
- * clip a string to a fixed length. If length is not specified, use 15 as default.
- * If a string is clipped, it's followed with a "...".
- * If text is null or undefined or empty, return the original value.
- * Throw an error if text is not a string, or length <=0
- */
- BartUtils.clipText = function(text, length)
- {
- if(!text)
- {
- // return the original value
- return text;
- }
-
- //
- if((typeof text != "string"))
- {
- throw new Error("text required");
- }
-
- if(length <= 0)
- {
- throw new Error("length should be greater than 0");
- }
-
- if(typeof length == "undefined")
- {
- length = 15;
- }
-
- var clippedText;
- if(text.length > length)
- {
- clippedText = text.substr(0, length) + "...";
- }
- else
- {
- clippedText = text;
- }
-
- return clippedText;
- };
-
- /**
- * trim leading and trailing whitespace.
- * If text is null or undefined or empty, return the original value.
- * Throw an error if text type is not a string.
- */
- BartUtils.trimText = function(text)
- {
- if(!text)
- {
- return text;
- }
- if(typeof(text) != "string")
- {
- throw new Error("text required");
- }
-
- text = text.replace(/^\s+/, '');
- text = text.replace(/\s+$/, '');
-
- // and replace all conjuncted whitespaces with a single space
- //text = text.replace(/\s+/g, ' ');
-
- return text;
- };
-
- /**
- * Log an error to Firefox's error console.
- */
- BartUtils.logError = function(error)
- {
- Components.utils.reportError(error);
- };
-
- /**
- * A utility function for defining javascript classes.
- *
- * This function expects a single object as its only argument.
- * It defines a new JavaScript class based on the data and returns the constructor function of the new class.
- *
- * The object passed as an argument should have some or all of the following properties:
- *
- * name: The name of the class being defined.
- * If specified, this value will be stored in the classname property of the prototype object.
- *
- * extend: The constructor of the class to be extended. If omitted. the Object() constructor will be used.
- * This value will be stored in the superclass property of the prototype object.
- *
- * constructor: The constructor function for the class. If omitted, a new empty function will be used.
- * This value becomes the return value of the function, and is also stored in the constructor
- * property of the prototype object.
- *
- * methods: An object that specifies the insstance methods (and other shared properties) for the class.
- * The properties of this object are copied into the prototype object of the class.
- * Properties named "classname", "superclass" and "constructor" are reserved and should not
- * be used in this object.
- *
- * statics: An object that specifies the static methods (and other static properties) for the class.
- * The properties of this object become properties of the constructor function.
- */
- BartUtils.defineClass = function(data)
- {
- var classname = data.name;
- var superclass = data.extend || Object;
- var constructor = data.construct || function(){};
- var methods = data.methods || {};
- var statics = data.statics || {};
-
- // create the prototype object that will become prototype for the result class
- var proto = new superclass();
-
- for(var p in proto)
- {
- // delete any noninherited properties
- if(proto.hasOwnProperty(p))
- {
- delete proto[p];
- }
- }
-
- // copy instance methods to the prototype object
- for(var p in methods)
- {
- proto[p] = methods[p];
- }
-
- proto.constructor = constructor;
- proto.superclass = superclass;
- if(classname)
- {
- proto.classname = classname;
- }
-
- // associate the prototype object with the constructor function
- constructor.prototype = proto;
-
- // copy static properties to the constructor
- for(var p in statics)
- {
- constructor[p] = statics[p];
- }
-
- return constructor;
- };
-
- /**
- * Create an XMLHttpRequest object.
- * The object passed as an argument should have some or all of the following properties:
- *
- * method: "GET" or "POST.
- * url: The url to which to send the request.
- * async: true or false. Default is true, indicating whether or not to perform the operation asynchronously.
- * body: This may be an nsIDocument, nsIInputStream, or a string (an nsISupportsString if called from native code)
- * that is used to populate the body of a POST request.
- * callback: callback function for "onreadystatechange" event if in async mode.
- *
- * This method will open the url and send data to that url if it's specified.
- *
- * An Error will be thrown if error occurs.
- */
- BartUtils.createXMLHttpRequest = function(data)
- {
- var method = data.method || "GET";
- var url = data.url;
- var async = (data.async == undefined ? true : data.async);
- var body = (data.body == undefined ? null : data.body);
- var callback = data.callback;
- var eventName = data.event || "onreadystatechange";
- var mimeType = data.mimeType || "text/xml";
-
- var httpRequest = new XMLHttpRequest();
- if(!httpRequest)
- {
- throw new Error("Failed to create XMLHttpRequest object.");
- }
-
- if(httpRequest.overrideMimeType)
- {
- httpRequest.overrideMimeType(mimeType);
- }
-
- if(callback)
- {
- //httpRequest.onreadystatechange = callback;
- httpRequest.addEventListener(eventName, callback, false);
- }
-
- if(url)
- {
- httpRequest.open(method, url, async);
- httpRequest.send(body);
- }
-
- return httpRequest;
- };
-
- BartUtils.registerCss = function(cssText)
- {
- var styleSheetService = Components.classes["@mozilla.org/content/style-sheet-service;1"]
- .getService(Components.interfaces.nsIStyleSheetService);
- var ioService = Components.classes["@mozilla.org/network/io-service;1"]
- .getService(Components.interfaces.nsIIOService);
-
- var uri = ioService.newURI("data:text/css;base64," + btoa(cssText), null, null);
- if(!styleSheetService.sheetRegistered(uri, styleSheetService.USER_SHEET))
- {
- styleSheetService.loadAndRegisterSheet(uri, styleSheetService.USER_SHEET);
- }
- };
-
- /**
- * Load a local file. filePath should have pattern: chrome://ExtensionName/...filename.
- * callback should be a function, signature is: func(event). Event target is an XMLHttpRequest object.
- */
- BartUtils.loadLocalFile = function(filePath, mimeType, callback)
- {
- BartUtils.createXMLHttpRequest(
- {
- url: filePath,
- async: false,
- event: "load",
- mimeType: mimeType,
- callback: function(event)
- {
- if(callback)
- {
- callback(event);
- }
- }
- });
- };
- /*
- BartUtils.str2Hex = function(s)
- {
- var c = "";
- var n;
- var ss = "0123456789ABCDEF";
- var digS = "";
- for(var i = 0; i < s.length; i ++)
- {
- c = s.charAt(i);
- n = ss.indexOf(c);
- digS += BartUtils.dec2Dig(eval(n));
-
- }
- //return value;
- return digS;
- };
-
- BartUtils.dec2Dig = function(n1)
- {
- var s = "";
- var n2 = 0;
- for(var i = 0; i < 4; i++)
- {
- n2 = Math.pow(2,3 - i);
- if(n1 >= n2)
- {
- s += '1';
- n1 = n1 - n2;
- }
- else
- s += '0';
-
- }
- return s;
- };
-
- BartUtils.dig2Dec = function(s)
- {
- var retV = 0;
- if(s.length == 4)
- {
- for(var i = 0; i < 4; i ++)
- {
- retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
- }
- return retV;
- }
- return -1;
- };
-
- BartUtils.hex2Utf8 = function(s)
- {
- var retS = "";
- var tempS = "";
- var ss = "";
- if(s.length == 16)
- {
- tempS = "1110" + s.substring(0, 4);
- tempS += "10" + s.substring(4, 10);
- tempS += "10" + s.substring(10,16);
- var sss = "0123456789ABCDEF";
- for(var i = 0; i < 3; i ++)
- {
- retS += "%";
- ss = tempS.substring(i * 8, (eval(i)+1)*8);
-
- retS += sss.charAt(BartUtils.dig2Dec(ss.substring(0,4)));
- retS += sss.charAt(BartUtils.dig2Dec(ss.substring(4,8)));
- }
- return retS;
- }
- return "";
- };
-
- BartUtils.UTF8Encode = function(s1)
- {
- var s = escape(s1);
- var sa = s.split("%");
- var retV ="";
- if(sa[0] != "")
- {
- retV = sa[0];
- }
- for(var i = 1; i < sa.length; i ++)
- {
- if(sa[i].substring(0,1) == "u")
- {
- retV += BartUtils.hex2Utf8(BartUtils.str2Hex(sa[i].substring(1,5)));
-
- }
- else retV += "%" + sa[i];
- }
-
- return retV;
- };
- */
-
- BartUtils.Base64 = {
-
- // private property
- _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
-
- encode: function(input)
- {
- var output = "";
- var byteBuffer;
- var encodedCharIndexes = new Array(4);
- var i = 0;
- var j;
- var paddingBytes = 0;
-
- while(i < input.length)
- {
- // Fill byte buffer array
- byteBuffer = new Array(3);
- for(j = 0; j < byteBuffer.length; j++)
- if(i < input.length)
- byteBuffer[j] = input.charCodeAt(i++) & 0xff; // throw away high-order byte, as documented at: https://developer.mozilla.org/En/Using_XMLHttpRequest#Handling_binary_data
- else
- byteBuffer[j] = 0;
-
- // Get each encoded character, 6 bits at a time
- // index 1: first 6 bits
- encodedCharIndexes[0] = byteBuffer[0] >> 2;
- // index 2: second 6 bits (2 least significant bits from input byte 1 + 4 most significant bits from byte 2)
- encodedCharIndexes[1] = ((byteBuffer[0] & 0x3) << 4) | (byteBuffer[1] >> 4);
- // index 3: third 6 bits (4 least significant bits from input byte 2 + 2 most significant bits from byte 3)
- encodedCharIndexes[2] = ((byteBuffer[1] & 0x0f) << 2) | (byteBuffer[2] >> 6);
- // index 3: forth 6 bits (6 least significant bits from input byte 3)
- encodedCharIndexes[3] = byteBuffer[2] & 0x3f;
-
- // Determine whether padding happened, and adjust accordingly
- paddingBytes = i - (input.length - 1);
- switch(paddingBytes){
- case 2:
- // Set last 2 characters to padding char
- encodedCharIndexes[3] = 64;
- encodedCharIndexes[2] = 64;
- break;
- case 1:
- // Set last character to padding char
- encodedCharIndexes[3] = 64;
- break;
- default:
- break; // No padding - proceed
- }
- // Now we will grab each appropriate character out of our keystring
- // based on our index array and append it to the output string
- for(j = 0; j < encodedCharIndexes.length; j++)
- output += this._keyStr.charAt(encodedCharIndexes[j]);
- }
- return output;
- }
- };